package com.lobo.blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lobo.blog.entity.Type;
import com.lobo.blog.exception.AllReadyBeenDeletedException;
import com.lobo.blog.exception.TypeNotFoundException;
import com.lobo.blog.mappers.AdminTypesMapper;
import com.lobo.blog.service.AdminTypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @authoer:MacieSerenity
 * @createDate: 2022/9/29
 * @description:
 */
@Service
public class AdminTypeServiceImpl implements AdminTypeService {

    @Resource
    AdminTypesMapper adminTypesMapper;

    @Override
    public int saveType(Type type) {
        if (type.getName() == null || "".equals(type.getName())) {
            System.out.println("名称为空");
            return 0;
        }
        Type exists = adminTypesMapper.findTypeByName(type.getName());
        if (exists != null) {
            Date now = new Date();
            exists.setUpdateTime(now);
            adminTypesMapper.updateTypeDeletedStatusToFalse(exists);
            return 1;
        } else {
            Date now = new Date();
            type.setCreateTime(now);
            type.setUpdateTime(now);
            return adminTypesMapper.addNewType(type);
        }
    }

    @Override
    public Type getTypeByTypeId(Integer typeId) throws TypeNotFoundException {
        Type type = adminTypesMapper.getTypeById(typeId);
        if (type != null) {
            return type;
        } else {
            throw new TypeNotFoundException();
        }
    }

    @Override
    public List<Type> getAllType() {
        return adminTypesMapper.getAllTypeByList();
    }

    @Override
    public PageInfo<Type> getTypes(Integer pageNum) {
        //开启分页功能（类似于拦截器，拦截下一条查询语句，并加上limit语句）
        PageHelper.startPage(pageNum, 10);
        List<Type> list = adminTypesMapper.getAllTypeByList();
        PageInfo page = new PageInfo(list);
        System.out.println(page);
        return page;
    }

    @Override
    public int updateType(Type type) {
        Type exists = adminTypesMapper.findTypeByName(type.getName());
        if (exists == null) {
            Date now = new Date();
            type.setUpdateTime(now);
            return adminTypesMapper.updateType(type);
        } else {
            int deleteStatus = exists.getIsDeleted();
            //是否被删除，非0为真
            if (deleteStatus == 1) {
                Date now = new Date();
                type.setUpdateTime(now);
                exists.setUpdateTime(now);
                adminTypesMapper.updateTypeDeletedStatusToFalse(exists);
                return adminTypesMapper.updateTypeDeletedStatusToTrue(type);
            } else {
                return 0;
            }
        }
    }

    @Override
    public int deleteTypeById(Integer id) throws AllReadyBeenDeletedException {
        int result = adminTypesMapper.deleteTypeById(id);
        if (result == 0) {
            throw new AllReadyBeenDeletedException();
        }
        return 1;
    }
}
